package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.common.CommonUtils;
import com.glink.manage.common.PoiTools;
import com.glink.manage.dto.log.LogExportDTO;
import com.glink.manage.dto.log.LogPageDTO;
import com.glink.manage.dto.log.LogSaveDTO;
import com.glink.manage.service.LogService;
import com.glink.manage.vo.log.LogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/17 17:28
 */
@Slf4j
@RestController
@RequestMapping("/API/Glink-Intelligent-Operation-User/log")
@Api(tags = "日志管理")
public class LogController extends AbstractBaseController{

    @Autowired
    private LogService logService;
    
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "01.日志信息分页查询", notes = "01.日志信息分页查询")
    @PostMapping(value = "/findLogPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<LogVO>> findLogPage(@RequestBody @Validated LogPageDTO pageDTO) throws GeneralSimpleException {
        log.info("01.日志信息分页查询：{}", LogUtil.encode(pageDTO));
        IPage<LogVO> logVOIPage = logService.findLogPage(pageDTO);
        log.info("01.日志信息分页查询：success");
        return this.response(logVOIPage);
    }

    @ApiOperation(value = "02.导出系统日志信息", notes = "02.导出系统日志信息")
    @PostMapping(value = "/exportLog")
    public void exportLog(@RequestBody @Validated LogExportDTO exportDTO, HttpServletResponse response) throws Exception {
        log.info("02.导出系统日志信息{}", LogUtil.encode(exportDTO));
        String filePath = logService.exportLog(exportDTO);
        String fileName = FilenameUtils.getName(filePath);
        PoiTools.exportExcelFile(filePath, fileName, response);
    }

    @ApiOperation(value = "03.添加系统日志", notes = "03.添加系统日志")
    @PostMapping(value = "/saveLogRecord", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的用户id", dataType = "string", required = true)
    public ResponseResult<Boolean> addLogRecord(@RequestBody @Validated LogSaveDTO saveDTO) throws Exception {
        log.info("03.添加系统日志{}", LogUtil.encode(saveDTO));
        String userId = CommonUtils.extractUserIdtByToken();
        Boolean status = logService.addLogRecord(userId, saveDTO);
        log.info("01.日志信息分页查询：success");
        return this.response(status);
    }
}
